using System.Collections.Generic;
using UnityEngine;

public class minimapcontroller : MonoBehaviour
{
	public static minimapcontroller instance;

	public Transform minimapObj;

	public Transform playerTra;

	private float playerYangle;

	public float showDis;

	private float radius;

	public UISprite sector;

	private Vector3 playervc3;

	private Vector3 calvc3;

	private Vector3 dir;

	public List<Transform> planeflag;

	public List<Transform> shipflag;

	public List<Transform> building;

	public List<Transform> planeflag_fri;

	public List<Transform> shipflag_fri;

	public List<Transform> building_fri;

	public Transform task_desflag;

	private Vector3 disappearpos = new Vector3(1000f, 1000f, 0f);

	private void Awake()
	{
		if (instance == null)
		{
			instance = this;
		}
	}

	public void findlist_init()
	{
		building.Clear();
		shipflag.Clear();
		planeflag.Clear();
		building_fri.Clear();
		shipflag_fri.Clear();
		planeflag_fri.Clear();
		foreach (Transform item in minimapObj)
		{
			switch (item.name)
			{
			case "building":
				building.Add(item);
				break;
			case "building_fri":
				building_fri.Add(item);
				break;
			case "planeflag":
				planeflag.Add(item);
				break;
			case "planeflag_fri":
				planeflag_fri.Add(item);
				break;
			case "shipflag":
				shipflag.Add(item);
				break;
			case "shipflag_fri":
				shipflag_fri.Add(item);
				break;
			}
		}
	}

	private void Start()
	{
		Vector2 localSize = sector.localSize;
		radius = localSize.x / 2f - 3f;
	}

	public void initinfo(Transform tra)
	{
		playerTra = tra;
	}

	private void Update()
	{
		Vector3 eulerAngles = playerTra.eulerAngles;
		playerYangle = eulerAngles.y;
		Transform transform = minimapObj;
		Vector3 localEulerAngles = minimapObj.localEulerAngles;
		transform.localEulerAngles = new Vector3(0f, 0f, Mathf.LerpAngle(localEulerAngles.z, playerYangle, Time.deltaTime * 8f));
		Vector3 position = playerTra.position;
		if (position.y > 1000f)
		{
			showDis = Mathf.Lerp(showDis, 3000f, Time.deltaTime * 2f);
		}
		else
		{
			showDis = Mathf.Lerp(showDis, 2000f, Time.deltaTime * 2f);
		}
	}

	public void fri_show_flag(AI_TYPE type, Vector3 pos, float Yangle, int index, bool isdie = false)
	{
		Vector3 position = playerTra.position;
		float x = position.x;
		Vector3 position2 = playerTra.position;
		playervc3 = new Vector3(x, position2.z, 0f);
		calvc3 = new Vector3(pos.x, pos.z, 0f);
		if (isdie)
		{
			switch (type)
			{
			case AI_TYPE.aiplane:
				planeflag_fri[index].localPosition = disappearpos;
				break;
			case AI_TYPE.aiship:
				shipflag_fri[index].localPosition = disappearpos;
				break;
			case AI_TYPE.building:
				building_fri[index].localPosition = disappearpos;
				break;
			case AI_TYPE.smallbuild:
				building_fri[index].localPosition = disappearpos;
				break;
			case AI_TYPE.aibunker:
				building_fri[index].localPosition = disappearpos;
				break;
			case AI_TYPE.aihgtw:
				building_fri[index].localPosition = disappearpos;
				break;
			case AI_TYPE.aiboss:
				building_fri[index].localPosition = disappearpos;
				break;
			}
			return;
		}
		if (Vector3.Distance(playervc3, calvc3) < showDis)
		{
			dir = (calvc3 - playervc3) / showDis;
		}
		else
		{
			dir = (calvc3 - playervc3).normalized;
		}
		switch (type)
		{
		case AI_TYPE.aiplane:
			planeflag_fri[index].localPosition = dir * radius + minimapObj.localPosition;
			planeflag_fri[index].localEulerAngles = new Vector3(0f, 180f, Yangle);
			break;
		case AI_TYPE.aiship:
			shipflag_fri[index].localPosition = dir * radius + minimapObj.localPosition;
			shipflag_fri[index].localEulerAngles = new Vector3(0f, 180f, Yangle);
			break;
		case AI_TYPE.building:
			building_fri[index].localPosition = dir * radius + minimapObj.localPosition;
			break;
		case AI_TYPE.smallbuild:
			building_fri[index].localPosition = dir * radius + minimapObj.localPosition;
			break;
		case AI_TYPE.aibunker:
			building_fri[index].localPosition = dir * radius + minimapObj.localPosition;
			break;
		case AI_TYPE.aihgtw:
			building_fri[index].localPosition = dir * radius + minimapObj.localPosition;
			break;
		case AI_TYPE.aiboss:
			building_fri[index].localPosition = dir * radius + minimapObj.localPosition;
			break;
		}
	}

	public void show_flag(AI_TYPE type, Vector3 pos, float Yangle, int index, bool isdie = false)
	{
		Vector3 position = playerTra.position;
		float x = position.x;
		Vector3 position2 = playerTra.position;
		playervc3 = new Vector3(x, position2.z, 0f);
		calvc3 = new Vector3(pos.x, pos.z, 0f);
		if (isdie)
		{
			switch (type)
			{
			case AI_TYPE.aiplane:
				planeflag[index].localPosition = disappearpos;
				break;
			case AI_TYPE.aiship:
				shipflag[index].localPosition = disappearpos;
				break;
			case AI_TYPE.building:
				building[index].localPosition = disappearpos;
				break;
			case AI_TYPE.smallbuild:
				building[index].localPosition = disappearpos;
				break;
			case AI_TYPE.aibunker:
				building[index].localPosition = disappearpos;
				break;
			case AI_TYPE.aihgtw:
				building[index].localPosition = disappearpos;
				break;
			case AI_TYPE.aiboss:
				building[index].localPosition = disappearpos;
				break;
			}
			return;
		}
		if (Vector3.Distance(playervc3, calvc3) < showDis)
		{
			dir = (calvc3 - playervc3) / showDis;
		}
		else
		{
			dir = (calvc3 - playervc3).normalized;
		}
		switch (type)
		{
		case AI_TYPE.aiplane:
			planeflag[index].localPosition = dir * radius + minimapObj.localPosition;
			planeflag[index].localEulerAngles = new Vector3(0f, 180f, Yangle);
			break;
		case AI_TYPE.aiship:
			shipflag[index].localPosition = dir * radius + minimapObj.localPosition;
			shipflag[index].localEulerAngles = new Vector3(0f, 180f, Yangle);
			break;
		case AI_TYPE.building:
			building[index].localPosition = dir * radius + minimapObj.localPosition;
			break;
		case AI_TYPE.smallbuild:
			building[index].localPosition = dir * radius + minimapObj.localPosition;
			break;
		case AI_TYPE.aibunker:
			building[index].localPosition = dir * radius + minimapObj.localPosition;
			break;
		case AI_TYPE.aihgtw:
			building[index].localPosition = dir * radius + minimapObj.localPosition;
			break;
		case AI_TYPE.aiboss:
			building[index].localPosition = dir * radius + minimapObj.localPosition;
			break;
		}
	}

	public void show_taskflag(Vector3 pos, bool isshow)
	{
		Vector3 position = playerTra.position;
		float x = position.x;
		Vector3 position2 = playerTra.position;
		playervc3 = new Vector3(x, position2.z, 0f);
		calvc3 = new Vector3(pos.x, pos.z, 0f);
		if (isshow)
		{
			NGUITools.SetActive(task_desflag.gameObject, state: true);
			if (Vector3.Distance(playervc3, calvc3) < showDis)
			{
				dir = (calvc3 - playervc3) / showDis;
			}
			else
			{
				dir = (calvc3 - playervc3).normalized;
			}
			task_desflag.localPosition = dir * radius + minimapObj.localPosition;
		}
		else
		{
			task_desflag.localPosition = disappearpos;
			NGUITools.SetActive(task_desflag.gameObject, state: false);
		}
	}

	private void Destroy()
	{
		if (instance != null)
		{
			instance = null;
		}
	}
}
